package com.xian.im.modules.im.core.impl;

import com.xian.im.common.constant.EhcacheConstant;
import com.xian.im.common.model.dto.GroupUserDto;
import com.xian.im.common.model.vo.UserVo;
import com.xian.im.modules.im.core.IGroupUserCoreService;
import com.xian.im.modules.im.core.IUserCoreService;
import com.xian.im.modules.im.entity.GroupUserEntity;
import com.xian.im.modules.im.service.GroupUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @program: xian-code
 * @description:
 * @author: liru.xian
 * @create: 2021-01-19 21:23
 **/
@Slf4j
@Service
public class IGroupUserCoreServiceImpl implements IGroupUserCoreService {

    @Autowired
    private GroupUserService groupUserService;
    @Autowired
    private IUserCoreService userCoreService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加群组管理员
     *
     * @param userId
     * @param groupId
     * @return  ture 存在  false 不存在
     */
    @Override
    @CacheEvict(cacheNames = EhcacheConstant.GROUP_USER)
    public Boolean addManager(String userId, String groupId) {
        if(exist(userId,groupId)){
            return Boolean.TRUE;
        }
        GroupUserEntity groupUserEntity = new GroupUserEntity();
        groupUserEntity.setRole(0);
        groupUserEntity.setStatus(0);
        groupUserEntity.setUserAccount(userId);
        groupUserEntity.setGroupUniqueId(groupId);
        groupUserEntity.setRemark("群管理员");
        return groupUserService.save(groupUserEntity);
    }

    /**
     * 删除群组管理员
     *
     * @param userId
     * @param groupId
     * @return
     */
    @Override
    @CacheEvict(cacheNames = EhcacheConstant.GROUP_USER)
    public Boolean removeManager(String userId, String groupId) {

        return groupUserService.lambdaUpdate().eq(GroupUserEntity::getUserAccount,userId).eq(GroupUserEntity::getGroupUniqueId,groupId).remove();
    }

    /**
     * 管理员是否存在
     *
     * @param userId
     * @param groupId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    @Cacheable(cacheNames  = EhcacheConstant.GROUP_USER)
    public Boolean exist(String userId, String groupId) {
        GroupUserEntity groupUserEntity = groupUserService.lambdaQuery().select(GroupUserEntity::getId) .eq(GroupUserEntity::getUserAccount, userId) .eq(GroupUserEntity::getGroupUniqueId, groupId).one();
        return null == groupUserEntity ? Boolean.FALSE: Boolean.TRUE;
    }

    /**
     * 获取群组所有的管理员
     *
     * @param groupId
     * @return
     */
    @Override
    @Cacheable(cacheNames  = EhcacheConstant.GROUP_USER)
    public Set<String> getUserAccountByGroupId(String groupId) {
        List<GroupUserEntity> list = groupUserService.lambdaQuery().select(GroupUserEntity::getUserAccount).eq(GroupUserEntity::getGroupUniqueId, groupId).list();
        return list.isEmpty() ? new HashSet<>() : list.stream().map(GroupUserEntity::getUserAccount).collect(Collectors.toSet());
    }

    /**
     * 获取群组所有的管理员
     *
     * @param groupId
     * @param orgId
     * @return
     */
    @Override
    @Cacheable(cacheNames  = EhcacheConstant.GROUP_USER)
    public Set<String> getUserAccountByGroupId(String groupId, Long orgId) {
        List<GroupUserEntity> list = groupUserService.lambdaQuery().select(GroupUserEntity::getUserAccount).eq(GroupUserEntity::getOrgId,orgId).eq(GroupUserEntity::getGroupUniqueId, groupId).list();
        return list.isEmpty() ? null : list.stream().map(GroupUserEntity::getUserAccount).collect(Collectors.toSet());
    }

    /**
     * 获取群组所有的管理员
     *
     * @param groupId
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    @Cacheable(cacheNames  = EhcacheConstant.GROUP_USER)
    public Set<UserVo> getUserInfoByGroupId(String groupId) {
        Set<String> userAccounts = getUserAccountByGroupId(groupId);
        if(!userAccounts.isEmpty()){
            List<UserVo> userVos = userCoreService.batchFindByAccount(userAccounts);
            Set<UserVo> sets = new HashSet<>();
            sets.addAll(userVos);
            return sets;
        }
        return new HashSet<>();
    }


    /**
     * 禁言用户
     *
     * @param shutUpTime   禁言时间 秒
     * @param groupUserDto 用户ID
     * @return
     */
    @Override
    public Boolean shutUpUser(Long shutUpTime, GroupUserDto groupUserDto) {
        String key  = CACHE_KEY+groupUserDto.getGroupUniqueId()+":"+"type"+":"+groupUserDto.getUserAccount();
        redisTemplate.opsForValue().set(key,1,shutUpTime,TimeUnit.SECONDS);
        return Boolean.TRUE;
    }

    /**
     * 解除禁言
     *
     * @param groupUserDto
     * @return
     */
    @Override
    public Boolean unmute(GroupUserDto groupUserDto) {
        String key = CACHE_KEY+groupUserDto.getGroupUniqueId()+":"+"type"+":"+groupUserDto.getUserAccount();
        redisTemplate.delete(key);
        return Boolean.TRUE;
    }

    /**
     * 是否是禁言用户
     *
     * @param groupUserDto
     * @return
     */
    @Override
    public Boolean existMute(GroupUserDto groupUserDto){
        String key = CACHE_KEY+groupUserDto.getGroupUniqueId()+":"+"type"+":"+groupUserDto.getUserAccount();
        Object o = redisTemplate.opsForValue().get(key);
       return   null  == o ? Boolean.FALSE : Boolean.TRUE;
    }

}

